<template>
  <div class="app-container">
    <div class="filter-container">
      <el-date-picker
        v-model="listQuery.month"
        type="month"
        placeholder="选择月份"
        class="filter-item"
        style="width: 200px"
      />
      <el-select
        v-model="listQuery.departmentId"
        placeholder="选择部门"
        clearable
        class="filter-item"
        style="width: 200px"
      >
        <el-option
          v-for="item in departmentOptions"
          :key="item.id"
          :label="item.name"
          :value="item.id"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >
        搜索
      </el-button>
    </div>

    <el-row :gutter="20">
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>出勤率</span>
            </div>
          </template>
          <div class="card-body">
            <div class="stat-value">{{ stats.attendanceRate }}%</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>迟到次数</span>
            </div>
          </template>
          <div class="card-body">
            <div class="stat-value">{{ stats.lateCount }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>早退次数</span>
            </div>
          </template>
          <div class="card-body">
            <div class="stat-value">{{ stats.earlyCount }}</div>
          </div>
        </el-card>
      </el-col>
      <el-col :span="6">
        <el-card class="box-card">
          <template #header>
            <div class="card-header">
              <span>缺勤次数</span>
            </div>
          </template>
          <div class="card-body">
            <div class="stat-value">{{ stats.absentCount }}</div>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <el-table
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%; margin-top: 20px;"
    >
      <el-table-column
        label="员工姓名"
        prop="employeeName"
        align="center"
        width="120"
      />
      <el-table-column
        label="部门"
        prop="departmentName"
        align="center"
        width="150"
      />
      <el-table-column
        label="应出勤天数"
        prop="totalDays"
        align="center"
        width="120"
      />
      <el-table-column
        label="实际出勤天数"
        prop="actualDays"
        align="center"
        width="120"
      />
      <el-table-column
        label="迟到次数"
        prop="lateCount"
        align="center"
        width="100"
      />
      <el-table-column
        label="早退次数"
        prop="earlyCount"
        align="center"
        width="100"
      />
      <el-table-column
        label="缺勤次数"
        prop="absentCount"
        align="center"
        width="100"
      />
      <el-table-column
        label="出勤率"
        prop="attendanceRate"
        align="center"
        width="100"
      >
        <template #default="{row}">
          {{ row.attendanceRate }}%
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="listQuery.page"
      :limit.sync="listQuery.limit"
      @pagination="getList"
    />
  </div>
</template>

<script>
import { defineComponent, ref, onMounted } from 'vue'
import waves from '@/directive/waves'
import Pagination from '@/components/Pagination'

export default defineComponent({
  name: 'AttendanceStats',
  components: { Pagination },
  directives: { waves },
  setup() {
    const list = ref([])
    const total = ref(0)
    const listLoading = ref(true)
    const listQuery = ref({
      page: 1,
      limit: 20,
      month: new Date(),
      departmentId: undefined
    })

    const departmentOptions = ref([])
    const stats = ref({
      attendanceRate: 95,
      lateCount: 5,
      earlyCount: 3,
      absentCount: 2
    })

    const getList = () => {
      listLoading.value = true
      // TODO: 调用API获取考勤统计数据
      setTimeout(() => {
        list.value = [
          {
            employeeName: '张三',
            departmentName: '技术部',
            totalDays: 22,
            actualDays: 20,
            lateCount: 1,
            earlyCount: 0,
            absentCount: 1,
            attendanceRate: 90.9
          }
        ]
        total.value = 1
        listLoading.value = false
      }, 500)
    }

    const handleFilter = () => {
      listQuery.value.page = 1
      getList()
    }

    onMounted(() => {
      getList()
    })

    return {
      list,
      total,
      listLoading,
      listQuery,
      departmentOptions,
      stats,
      handleFilter
    }
  }
})
</script>

<style lang="scss" scoped>
.box-card {
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }

  .card-body {
    text-align: center;
    padding: 20px 0;

    .stat-value {
      font-size: 24px;
      font-weight: bold;
      color: #409EFF;
    }
  }
}
</style>